import { ref } from 'vue'
import auth from './auth'
import uiInstall, { ui } from './ui'
import router from './router'
import lang from './lang'
import plugin from './plugin'
import permission from './permission'
import time from './time'
import money from './money'
import math from './math'
import pdf from './pdf'
import imgurl from './imgurl'
import img from './img'
import task from './task'

export default {
  run(app) {
    app.use(uiInstall)
    app.use(router)
    app.use(lang.i18n)
    app.mount('#app')
  },

  login(token) {
    auth.setToken(token)
  },

  logout() {
    auth.clearToken()
    router.push('/')
  },

  isLogin() {
    return auth.isLogin()
  },

  token() {
    return auth.getToken()
  },

  number(data) {
    if(data !== undefined) auth.setNumber(data)

    return auth.getNumber()
  },

  username(data) {
    if(data !== undefined) auth.setUsername(data)

    return auth.getUsername()
  },

  message(type, message) {
    ui.MessagePlugin(type, typeof message == 'object' ? lang.t(...message) : lang.t(message), 2000)
  },

  messageLoading(message) {
    return ui.MessagePlugin('loading', typeof message == 'object' ? lang.t(...message) : lang.t(message), 0)
  },

  messageClose(msg) {
    ui.MessagePlugin.close(msg)
  },

  loading(type = true) {
    ui.LoadingPlugin(type)
  },

  notify(theme, title, content, options = {}) {
    options.title = title
    options.content = content
    ui.NotifyPlugin(theme, options)
  },

  confirm(options, func, cancel = null) {
    const loading = ref(false)
    options.confirmBtn.loading = loading
    options.onConfirm = (e) => {
      loading.value = true
      func(() => {
        loading.value = false 
        confirm.hide()
      })
    }
    if(cancel) {
      options.onCancel = cancel
    }
    const confirm = ui.DialogPlugin.confirm(options)
    return confirm
  },

  alert(options, func) {
    const loading = ref(false)
    options.confirmBtn.loading = loading
    options.onConfirm = (e) => {
      loading.value = true
      func(() => {
        loading.value = false 
        alert.hide()
      })
    }
    const alert = ui.DialogPlugin.alert(options)
    return alert
  },

  url(path) {
    return location.origin + location.pathname + '#/' + path
  },
  task(...args) {
    return new task(...args)
  },

  img,
  plugin,
  permission,
  router,
  lang,
  time,
  money,
  math,
  pdf,
  imgurl
}